import 'package:flutter/material.dart';
import 'package:yurun_smartmerchants/models/tallied_response_entity_entity.dart';
import 'package:yurun_smartmerchants/models/tobetally_response_entity.dart';
import 'package:yurun_smartmerchants/net/ApiInterface.dart';
import 'package:yurun_smartmerchants/page/tally_again_page.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/utils/app_size.dart';

//已进场列表
class TalliedList extends StatefulWidget {
  @override
  _TalliedListState createState() => _TalliedListState();
}

class _TalliedListState extends State<TalliedList> {
  List<TalliedResponseEntityContent> items = List();
  TalliedResponseEntityEntity talliedResponseEntityEntity;
  TobetallyResponseResponse tobetallyResponseResponse;

  @override
  void initState() {
    super.initState();
    requestTallyList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      color: Colours.gray_ef,
      child: Container(
        margin: EdgeInsets.only(top: AppSize.sp(20)),
        child: ListView.builder(
            itemBuilder: (context, index) {
              return getItem(context, index);
            },
            itemCount: items.length),
      ),
    );
  }

  Widget getItem(BuildContext context, int index) {
    return Container(
      margin: EdgeInsets.only(bottom: AppSize.sp(20)),
      child: Column(
        children: <Widget>[
          Container(
            color: Colours.color_f8f8f8,
            width: double.infinity,
            height: AppSize.height(64),
            alignment: Alignment.center,
            child: Row(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(
                      left: AppSize.sp(19), right: AppSize.sp(16)),
                  width: AppSize.width(5),
                  height: AppSize.height(28),
                ),
                Text(
                  "批次号: "+items[index].batch.toString(),
                  style: TextStyle(
                      color: Colours.color_313131, fontSize: AppSize.sp(32)),
                ),
                Spacer(),
                Container(
                  margin: EdgeInsets.only(right: AppSize.sp(32)),
                  child: Text(
//                    items[index].createdTime.substring(5),
                  '08-12 13:25',
                    style: TextStyle(
                        fontSize: AppSize.sp(26), color: Colours.color_898989),
                  ),
                )
              ],
            ),
          ),
          Container(
            width: double.infinity,
            height: AppSize.height(178),
            color: Colours.white,
            alignment: Alignment.center,
            child: Row(
             children: <Widget>[
               Container(
                  margin: EdgeInsets.only(left: AppSize.sp(16),top: AppSize.sp(16),bottom: AppSize.sp(16)),

                 child:Image.network(
                   items[index].url,
                   width: AppSize.width(178),
                   height: AppSize.height(146),
                   fit: BoxFit.cover,
                 ),
                ),
               Container(
                  margin: EdgeInsets.only(left:AppSize.sp(10), top: AppSize.sp(32)),
                  child:Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(
                        items[index].name,
                        style: TextStyle(fontSize: 18),),
                      Text(items[index].postWeight.toString() +'/公斤',style: TextStyle(color: Colors.red,),),
                    ],
                  ),
                ),
                Spacer(),
               Container(
                  margin: EdgeInsets.only(right: AppSize.sp(30)),
                  child:RaisedButton(
                    child: Text('重新理货'),
                    color: Colors.red,
                    textColor: Colors.white,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(5),
                    ),
                    onPressed: () {
                      Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                        return TallyAgainPage(content: items[index]);
                      }));
                    }
                  ),
                ),
             ],
            ),
          ),
        ],
      ),
    );
  }


  void requestTallyList() async {
    Map<String, dynamic> parameter = new Map();
    parameter['status'] = 1;
    parameter['pageNum'] = 0;
    parameter['pageSize'] = 15;
    ApiInterface.tallyList(parameter).then((data) {
      talliedResponseEntityEntity = TalliedResponseEntityEntity().fromJson(data);
      handleData(talliedResponseEntityEntity);
    }).catchError((error) {

    });
  }

  void handleData(TalliedResponseEntityEntity talliedResponseEntityEntity) {
    if(talliedResponseEntityEntity.content != null && talliedResponseEntityEntity.content.length > 0){
      items = talliedResponseEntityEntity.content;
    }else{
      //数据为空的处理

    }
    setState(() {});
  }





}
